Ext.define('Usws.controller.UserAttr', {
    extend: 'Ext.app.Controller',
    stores:['UserAttr'],
    
    refs: [
   		   {
               ref: 'contentPanel',
               selector: 'contentPanel'
           }
    ],

    init: function() {
        this.control({
            'UserAttr-Grid': {
                itemdblclick: this.editUserAttr,
                add: this.loadUserAttr
            },
            'UserAttr-Grid button[action=add]': {
                click: this.addUserAttr
            },
            
            'UserAttr-Grid button[action=deleteMore]':{  
                click: this.deleteUserAttrMore
            },
            
            'UserAttr-Edit button[action=save]': {
                click: this.updateUserAttr
            },
            'UserAttr-Edit': {
                close: this.closeUpdateUserAttr
            },
            'UserAttr-Grid actioncolumn':{
            	itemclick: this.actionColumnOptUserAttr
             }
        });
    },
    
    loadUserAttr:function(obj, component, index, eOpts){
    	var userAttrStore = this.getUserAttrStore();
    	userAttrStore.on('beforeload', function (store, options) {
          var new_params = { name: 'fengyong'/* Ext.get('search').getValue()*/ };
          Ext.apply(store.proxy.extraParams, new_params);
        });
    	userAttrStore.load();
    },
    
    addUserAttr: function(button) {
   	    var view = Ext.widget('UserAttr-Edit');
        var contentPanel = this.getContentPanel();
        contentPanel.setDisabled(true);
   },

    editUserAttr: function(grid, record) {
    	 var view = Ext.widget('UserAttr-Edit');
         view.down('form').loadRecord(record);
         var contentPanel = this.getContentPanel();
         contentPanel.setDisabled(true);
         var deptStore = this.getDeptStore();
         deptStore.load();
    },
    
    actionColumnOptUserAttr: function (column, grid, rowIndex, colIndex, item, e){
        var rec = grid.getStore().getAt(rowIndex);
        if (item.action == 'edit') {  
        	 this.editUserAttr(grid, rec);
        } else if (item.action == 'delete') {  
             this.deleteUserAttr(rec);
        }  
    },
    
    reloadUserAttr:function() {
    	var contentPanel = this.getContentPanel();
    	var userAttrStore = this.getUserAttrStore();
    	contentPanel.removeAll(true);
    	userAttrStore.load();
        contentPanel.add(Ext.widget('UserAttr-Grid'));
        if (contentPanel.floating) {
       	 contentPanel.show();
         }
        contentPanel.setDisabled(false);
    },
    
    updateUserAttr: function(button) {
    	var win = button.up('window'),
        form   = win.down('form');
    	var obj = this;
        form.submit({
        	method:'post',
            url: '/userattr/ajaxUpdateUserAttr.action',
            success: function(form, action) {
            	 win.close();
            	 obj.reloadUserAttr();
            },
            failure: function(form, action) {
            	Ext.Msg.alert("提示","更新失败，请重试！");  
            }
        });
    },
    
    deleteUserAttrMore: function(button) {
	    var userAttrGrid = button.ownerCt.ownerCt;  
        var data = userAttrGrid.getSelectionModel().getSelection();  
        if(data.length == 0){  
            Ext.Msg.alert("提示","您最少要选择一条数据");  
        }else{  
         	this.deleteUserAttr(data);
        }  
     },
     
     deleteUserAttr: function(data) {
    	 var obj = this;
    	 Ext.MessageBox.confirm("注意", "您真的要做删除操作吗?", function (btn) {
    		 if(btn === 'yes') {
    			 var ids = [];
    	    	 Ext.Array.each(data,function(record){  
    	             ids.push(record.get('uid'));  
    	         });  
    	         Ext.Ajax.request({  
    	             url:'/userattr/ajaxDeleteUserAttr.action',  
    	             params:{uids:ids.join(",")},  
    	             method:'POST',  
    	             timeout:2000,  
    	             success:function(response,opts){  
    	             	obj.reloadUserAttr();
    	             }  
    	         });  
    		 }
    	 });
     },
    
    closeUpdateUserAttr: function(panel, eOpts) {
 	     var contentPanel = this.getContentPanel();
         contentPanel.setDisabled(false);
    }
});